﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;

    public class PCMaintenance : BasePage
    {
        protected Button btnDelete;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chb;
        protected HtmlForm F5551;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPrID;
        protected HtmlInputHidden hdnTagNo;
        protected Label lblCountDate;
        protected Label lblCountQty;
        protected Label lblLoc;
        protected Label lblLot;
        protected Label lblLpnID;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblPackID;
        protected Label lblPhysicalRequestID;
        protected Label lblSkuID;
        protected Label lblTagNo;
        protected Label lblTeamNo;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCountDate;
        protected TextBox txtCountQty;
        protected TextBox txtLoc;
        protected TextBox txtLot;
        protected TextBox txtLpnID;
        protected TextBox txtOwnerID;
        protected TextBox txtPackID;
        protected TextBox txtPhysicalRequestID;
        protected TextBox txtSkuID;
        protected TextBox txtTagNo;
        protected TextBox txtTeamNo;
        protected TextBox txtUomID;
        protected UpdatePanel up1;

        protected void btnDelete_Click(object serder, EventArgs e)
        {
            try
            {
                List<string> IDS = new List<string> {
                    this.txtPhysicalRequestID.Text
                };
                List<string> TagNo = new List<string> {
                    this.txtTagNo.Text
                };
                this.lblMsg.Text = StockCountServiceClientProxy.GetInstance().DeletePhysicalTag(IDS, TagNo) + base.GetGlobalMsgResource("C006");
                this.hdnFlag.Value = "";
                this.txtPhysicalRequestID.Enabled = true;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtCountQty.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5770_001");
                    this.lblMsg.Visible = true;
                }
                else if (string.IsNullOrEmpty(this.txtCountDate.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5770_002");
                    this.lblMsg.Visible = true;
                }
                else if (!string.IsNullOrEmpty(this.hdnFlag.Value))
                {
                    this.UpdatePhysicalTag();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void GetNextTagNo()
        {
            List<string> TagNoList = base.PageQueryEntityViewState as List<string>;
            if ((TagNoList != null) && (TagNoList.Count > 0))
            {
                for (int i = 0; TagNoList.Count > i; i++)
                {
                    if ((TagNoList[i] == this.hdnTagNo.Value) && (TagNoList.Count > (i + 1)))
                    {
                        this.hdnTagNo.Value = TagNoList[i + 1];
                        break;
                    }
                }
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnPrID.Value = base.Request.QueryString["ID"];
                List<string> TagNoList = StockCountServiceClientProxy.GetInstance().GetPhysicalTagNoListByID(this.hdnPrID.Value);
                if ((TagNoList != null) && (TagNoList.Count > 0))
                {
                    base.PageQueryEntityViewState = TagNoList;
                }
                this.hdnTagNo.Value = base.Request.QueryString["TagNo"];
                this.hdnFlag.Value = "1";
                this.SetPhysicalTagInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["detail"].ToString() + ",2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPhysicalTagInfo2Form()
        {
            try
            {
                string PhysicalRequestID = this.hdnPrID.Value.ToString();
                int tagNo = Convert.ToInt32(this.hdnTagNo.Value);
                PhysicalTagInfo physicalTagInfo = StockCountServiceClientProxy.GetInstance().GetPhysicalTagInfoByID(PhysicalRequestID, tagNo);
                base.DataEntityViewState = physicalTagInfo;
                if (physicalTagInfo != null)
                {
                    this.txtPhysicalRequestID.Text = physicalTagInfo.PhysicalRequestID;
                    this.txtPhysicalRequestID.Enabled = false;
                    this.txtTeamNo.Text = physicalTagInfo.TeamNo.ToString();
                    this.txtTagNo.Text = physicalTagInfo.TagNo.ToString();
                    this.txtOwnerID.Text = physicalTagInfo.OwnerID;
                    this.txtSkuID.Text = physicalTagInfo.SkuID;
                    this.txtLot.Text = physicalTagInfo.Lot;
                    this.txtLpnID.Text = physicalTagInfo.LpnID;
                    this.txtPackID.Text = physicalTagInfo.PackID;
                    this.txtLoc.Text = physicalTagInfo.Loc;
                    this.txtUomID.Text = physicalTagInfo.UomID;
                    if (physicalTagInfo.CountDate != new DateTime())
                    {
                        this.txtCountDate.Text = base.GetPreferenceShortDate(physicalTagInfo.CountDate);
                    }
                    else
                    {
                        this.txtCountDate.Text = base.GetPreferenceShortDate(DateTime.Now);
                    }
                    this.txtCountQty.Text = base.GetPreferenceDigit(physicalTagInfo.CountQty);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdatePhysicalTag()
        {
            try
            {
                PhysicalTagInfo physicalTagInfo = base.DataEntityViewState as PhysicalTagInfo;
                if (!string.IsNullOrEmpty(this.txtCountQty.Text))
                {
                    physicalTagInfo.CountQty = Convert.ToDecimal(this.txtCountQty.Text);
                }
                physicalTagInfo.Status = "Counted";
                string strValue = StockCountServiceClientProxy.GetInstance().UpdatePhysicalTag(physicalTagInfo);
                if (this.chb.Checked)
                {
                    this.GetNextTagNo();
                    this.SetPhysicalTagInfo2Form();
                }
                if (!string.IsNullOrEmpty(strValue))
                {
                    this.lblMsg.Text = this.txtPhysicalRequestID.Text + "-" + strValue + base.GetGlobalMsgResource("C002");
                    this.lblMsg.Visible = true;
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C003");
                    this.lblMsg.Visible = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }
    }
}

